// ==UserScript==
// @name        Highlight
// @namespace   Highlight
// @version     1
// @include     http://ae*.tribalwars.ae/game.php?village=*&mode=reservations&screen=ally
// @grant       none
// ==/UserScript==



var e = document.getElementsByTagName('form')[1];

var d = document.createElement('div');
d.innerHTML = e.innerHTML;

e.parentNode.insertBefore(d, e);
e.parentNode.removeChild(e);


$("#reservations div thead tr").append("<th>تظليل على الخريطة</th>");
$("#reservations div tfoot tr").append("<td><input id='all_coler' class='btn' type='button' value='تظليل الكل' name='all_coler'></input></td>");
$(function(){
$("#all_coler").click(function(){
$("#reservations div table tbody tr td:nth-child(8)").find(".btn_color").click();
});
});

$(function(){
    
    var village = document.URL.match(/village=\d+/);
    var village2 = $("#reservations div tbody tr td:eq(1) span").find("a:eq(0)").attr("href").match(/\d+/g)[1];
    var url1 = '/game.php?'+village+'&village_id='+village2+'&screen=village_color';
    
    $.ajax({
        url : url1,
        datatype: "xml",
        success : function (data){
            
            var m = $(data).find("#content_value").find("form").attr("action").match(/h=\S+/);
            
            $("#reservations div tbody tr ").each(function(){
                $(this).append("<td></td>");
                var village3 = $(this).find("td:eq(1) span a:eq(0)").attr("href").match(/\d+/g)[1];
                var village4 = document.URL.match(/village=\d+/);
                var url9 = '/game.php?'+village4+'&village_id='+village3+'&screen=village_color';
                var url8 = '/game.php?'+village4+'&village_id='+village3+'&action=create_group&'+m;
                
                $(this).find("td:eq(7)").html("<form action='' method='post' id='color_form' target='_blank'><input name='village_id' value='' type='hidden' id='villageB'><input name='name' value='' type='hidden' id='villagec'><input class='btn btn_color' value='تظليل' type='submit' id='btn_color' class='btn_color'></form>");
                $(this).find("#color_form").attr("action",url8);
                $(this).find("#villageB").attr("value",village3);
                $(this).find("#villagec").attr("value",village3);  

              
                
            });
        }
    });
});



